﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using EasyBuildingCMS.BLL.CoreEngine;

namespace EasyBuildingCMS.BuildingBlocks.Core.Admin
{
    public partial class PageFormView : System.Web.UI.UserControl
    {
        protected PageRepository pageRepository;
        protected MasterPageRepository masterPageRepository;

        public string Mode {
            get {
                return HttpContext.Current.Request.QueryString["mode"];
            }
        }

        public string PageID
        {
            get
            {
                return HttpContext.Current.Request.QueryString["pageID"];
            }
        }

        public string ParentPageID
        {
            get
            {
                return HttpContext.Current.Request.QueryString["parentPageID"];
            }
        }

        public string ParentPageText
        {
            get
            {
                return HttpContext.Current.Request.QueryString["parentPageText"];
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack) 
            {
                init();
                this.EditPanel.Visible = true;
                this.ViewPanel.Visible = false;
                if (this.Mode.Equals("Edit"))
                {
                   LoadItemsForEdit();
                }
                else if (this.Mode.Equals("View"))
                {
                    this.EditPanel.Visible = false;
                    this.ViewPanel.Visible = true;
                    LoadItemsForView();
                }
            }
            

        }

        protected void init() {
            masterPageRepository = new MasterPageRepository();
            List<BLL.CoreEngine.MasterPage> masterPages = masterPageRepository.GetAcitveMasterPages();

            this.MasterPageDropDownList.Items.Insert(0, new ListItem("Please Select...", "Null"));
            foreach (BLL.CoreEngine.MasterPage masterPage in masterPages){
                this.MasterPageDropDownList.Items.Add(new ListItem(masterPage.MasterPageFriendlyName, masterPage.MasterPageID.ToString()));
            }
        }

        private void LoadItemsForEdit() {

            pageRepository = new PageRepository();
            BLL.CoreEngine.Page page = pageRepository.GetPageByID(Int32.Parse(this.PageID));

            this.PageIDLabel.Text = page.PageID.ToString();
            if (page.ParentPage != null)
                this.ParentPageLabel.Text = page.ParentPage.PageNavigationText;
            else
                this.ParentPageLabel.Text = "Root";
            this.PageTitleTextBox.Text = page.PageTitle;
            this.PageDescriptionTextBox.Text = page.PageDescription;
            this.VirtualHandleNameTextBox.Text = BLL.Helper.GetVirtualHandlerNameFromURL(page.VirtualHandleName);
            this.PageNavigationTextTextBox.Text = page.PageNavigationText;
            this.MasterPageDropDownList.SelectedValue = page.MasterPageID.ToString();
            this.InternalRedirectionURLTextBox.Text = page.InternalRedirectionURL;
            this.ExternalRedirectionURLTextBox.Text = page.ExternalRedirectionURL;
            this.PageMetaDescriptionTextBox.Text = page.PageMetaDescription;
            this.PageMetaKeywordsTextBox.Text = page.PageMetaKeywords;
            this.ApprovedCheckBox.Checked = page.Approved;
            this.PublishDateCalendarExtender.SelectedDate = page.PublishDate;
            this.ExpireDateCalendarExtender.SelectedDate = page.ExpireDate;
            this.CssClassTextBox.Text = page.CssClass;
            this.VisibleCheckBox.Checked = page.Visible;

        }

        private void LoadItemsForView()
        {

            PageRepository pageRepository = new PageRepository();
            BLL.CoreEngine.Page page = pageRepository.GetPageByID(Int32.Parse(this.PageID));

            //this.PageIDLabelView.Text = page.PageID.ToString();
            if (String.IsNullOrEmpty(this.ParentPageText))
                this.ParentPageLabelView.Text = "Root";
            else
                this.ParentPageLabelView.Text = this.ParentPageText;
            this.PageTitelLabelView.Text = page.PageTitle;
            this.PageDescriptionLabelView.Text = page.PageDescription;
            this.VirtualHandleNameView.Text = BLL.Helper.GetVirtualHandlerNameFromURL(page.VirtualHandleName);
            this.NavigationLabelView.Text = page.PageNavigationText;
            this.MasterPageLabelView.Text = page.MasterPage.MasterPageFriendlyName;
            this.InternalRedirectionURLLabelView.Text = page.InternalRedirectionURL;
            this.ExternalRedirectionURLLabelView.Text = page.ExternalRedirectionURL;
            this.MetaDescriptionLabelView.Text = page.PageMetaDescription;
            this.MetaKeywordsLabelView.Text = page.PageMetaKeywords;
            this.ApprovedCheckBox.Checked = page.Approved;
            this.PublishDateLabelView.Text = page.PublishDate.ToString();
            this.ExpireDateLabelView.Text = page.ExpireDate.ToString();
            this.CssClassTextBox.Text = page.CssClass;
            this.CreatedByLabelView.Text = page.CreatedBy;
            this.CreatedDateLabelView.Text = page.CreatedDate.ToString();
            this.LastRevisitedByLabelView.Text = page.LastRevisionUserName;
            this.LastRevisonDateLabelView.Text = page.LastRevisonDate.ToString();
            this.ApprovedLabelView.Text = page.Approved.ToString();
            this.ApprovedByLabelView.Text = page.ApprovedBy;
            this.ApprovedDateLabelView.Text = page.ApprovedDate.ToString();
            this.PublishDateLabelView.Text = page.PublishDate.ToString();
            this.ExpireDateLabelView.Text = page.ExpireDate.ToString();
            this.SortOrderLabelView.Text = page.SortOrder.ToString();
            this.CssClassLabelView.Text = page.CssClass;
            this.VisibleLabelView.Text = page.Visible.ToString();
        
        }

        protected void MasterPageCustomValidator_ServerValidate(object source, ServerValidateEventArgs args)
        {
            if (this.MasterPageDropDownList.SelectedValue.Equals("Null"))
            {
                args.IsValid = false;
            }
            else 
            {
                args.IsValid = true;
            }
        }

        protected void SubmitButton_Click(object sender, EventArgs e)
        {
            pageRepository = new PageRepository();

            string url = "~/page_" + this.VirtualHandleNameTextBox.Text + ".aspx";

            if (this.Mode.Equals("Insert"))
            {

                pageRepository.InsertPage(0, Int32.Parse(this.ParentPageID), url, this.PageTitleTextBox.Text,
                    this.PageDescriptionTextBox.Text, this.PageNavigationTextTextBox.Text, Int32.Parse(this.MasterPageDropDownList.SelectedValue), this.InternalRedirectionURLTextBox.Text,
                    this.ExternalRedirectionURLTextBox.Text, this.PageMetaDescriptionTextBox.Text, this.PageMetaKeywordsTextBox.Text, this.ApprovedCheckBox.Checked, BLL.Helper.ConvertStringToDateTime(this.PublishDateTextBox.Text),
                    BLL.Helper.ConvertStringToDateTime(this.ExpireDateTextBox.Text), 100, this.CssClassTextBox.Text, this.VisibleCheckBox.Checked);
            }
            else if (this.Mode.Equals("Edit"))
            {
                pageRepository.UpdatePage(Int32.Parse(this.PageID), url, this.PageTitleTextBox.Text,
                    this.PageDescriptionTextBox.Text, this.PageNavigationTextTextBox.Text, Int32.Parse(this.MasterPageDropDownList.SelectedValue), this.InternalRedirectionURLTextBox.Text,
                    this.ExternalRedirectionURLTextBox.Text, this.PageMetaDescriptionTextBox.Text, this.PageMetaKeywordsTextBox.Text, this.ApprovedCheckBox.Checked, BLL.Helper.ConvertStringToDateTime(this.PublishDateTextBox.Text),
                    BLL.Helper.ConvertStringToDateTime(this.ExpireDateTextBox.Text), 100, this.CssClassTextBox.Text, this.VisibleCheckBox.Checked);
                
            }

            string script = "<script language=\"javascript\">window.close();</script>";
            Parent.Page.RegisterStartupScript("clientScript", script);
            
        }

    }
}